package com.deyuanyun.pic.integalz.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.deyuanyun.pic.business.controller.dto.PipeLineNewTaskVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.integalz.controller.dto.IntegalzQueryVO;
import com.deyuanyun.pic.integalz.enumeration.InspTableNameEnum;
import com.deyuanyun.pic.integalz.service.PipeLineInspTaskService;
import com.deyuanyun.pic.pipe.controller.BaseController;
import com.deyuanyun.pic.pipe.domain.PipeSegmentInfo;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;

/**
 *  完整性分析Controller
 *
 * @author ChenKui
 * @date 2016-06-16
 */
@Controller
@RequestMapping("/integalz/inspTask")
public class IntegalzInspTaskController extends BaseController{

    private @Resource PipeLineInspTaskService pipeLineInspTaskService;

    //进入内腐蚀分析
    @RequestMapping("/init.html")
    public ModelAndView init(ModelMap model) {
        
        return forword("/integalz/insideCorrode", model);
    }
    
    /**/
    @RequestMapping("/queryInspTask.json")
    @ResponseBody
    public void queryInspTask(@RequestParam("pipeLineId") String pipeLineId,PipeLineNewTaskVO pipeLineNewTask){
        
        List<Map<String,Object>> list = pipeLineInspTaskService.findInspTask(pipeLineId, pipeLineNewTask,true);
        AjaxSupport.sendSuccessText("",list); 
    }
    
    @RequestMapping("/queryPigTask.json")
    @ResponseBody
    public void queryPigTask(@RequestParam("pipeLineId") String pipeLineId,PipeLineNewTaskVO pipeLineNewTask){
        
        Map<String,Object> map = null;
        List<Map<String,Object>> list = pipeLineInspTaskService.findInspTask(pipeLineId, pipeLineNewTask,null);
        if(list != null && !list.isEmpty()){
            map = list.get(0);
            //获取对应管段...主要是为了查询输送介质
            PipeSegmentInfo pipeSegmentInfo = pipeLineInspTaskService.querySegmentInfo(pipeLineId, pipeLineNewTask.getStartMileage());
            String MEDIUM_TYPE = CodeUtil.getName(pipeSegmentInfo.getMediumType());//获取输送介质
            map.put("MEDIUM_TYPE", MEDIUM_TYPE);
        }
        AjaxSupport.sendSuccessText("",map); 
    }

    /**
     * 获取金属损失_分布
     * @param integalzQuery
     */
    @RequestMapping("/query{metaLossType}MetaLoss.json")
    @ResponseBody
    public void queryInspMetaLoss(IntegalzQueryVO integalzQuery,@PathVariable("metaLossType")String metaLossType){
        
        String pipeLineId = integalzQuery.getPipeLineId();
        List<PipeLineNewTaskVO> pipeLineInspTaskList = pipeLineInspTaskService.queryNewInspTaskMerge(pipeLineId);
        List<Map<String,Object>> list = null;
        if("Insp".equals(metaLossType)){
            list = pipeLineInspTaskService.queryInspMetaLoss(integalzQuery, pipeLineInspTaskList);
        } else if("Exinsp".equals(metaLossType)){
            list = pipeLineInspTaskService.queryExinspMetaLoss(integalzQuery, pipeLineInspTaskList);
        } else{
            Asserts.assertTrue(false, "无法获取金属损失类型");
        }
        
        AjaxSupport.sendSuccessText("",list);
    }
    

    /**
     * 获取内部金属损失_趋势
     * @param integalzQuery
     */
    @RequestMapping("/query{metaLossType}MetaLossTrend.json")
    @ResponseBody
    public void queryInspMetaLossTrend(IntegalzQueryVO integalzQuery,@RequestParam("scales") BigDecimal[] scales
            ,@PathVariable("metaLossType")String metaLossType){
        Asserts.assertTrue(scales != null && scales.length > 1, "参数错误：集合点个数必须大于1个。");
        String pipeLineId = integalzQuery.getPipeLineId();
        
        List<PipeLineNewTaskVO> pipeLineInspTaskList1 = pipeLineInspTaskService.queryNewInspTaskMerge(pipeLineId);
        List<PipeLineNewTaskVO> pipeLineInspTaskList2 = pipeLineInspTaskService.qeurySecondInspTaskMerge(pipeLineId, pipeLineInspTaskList1);
        
        List<Map<String,Object>> list1 = null;
        List<Map<String,Object>> list2 = null;
        if("Insp".equals(metaLossType)){
            list1 = pipeLineInspTaskService.queryInspMetaLoss(integalzQuery, pipeLineInspTaskList1);
            list2 = pipeLineInspTaskService.queryInspMetaLoss(integalzQuery, pipeLineInspTaskList2);
        } else if("Exinsp".equals(metaLossType)){
            list1 = pipeLineInspTaskService.queryExinspMetaLoss(integalzQuery, pipeLineInspTaskList1);
            list2 = pipeLineInspTaskService.queryExinspMetaLoss(integalzQuery, pipeLineInspTaskList2);
        } else{
            Asserts.assertTrue(false, "无法获取金属损失类型");
        }
        
        List<Object[]> c_list = new ArrayList<Object[]>();
        for (int i = 1; i < scales.length; i++) {
            Object[] arr = new Object[3];
            arr[0] = scales[i - 1];
            
            BigDecimal scale = scales[i];
            for (int j = 0; j < list1.size(); j++) {//循环第一个金属损失
                Map<String,Object> map = list1.get(j);
                BigDecimal mileage = (BigDecimal)map.get("MILEAGE");
                
                if(scale.compareTo(mileage) == -1){
                    list1 = list1.subList(j, list1.size());
                    arr[2] = j ;
                    break;
                }
                else if(j == list1.size() - 1){
                    list1 = Collections.emptyList();
                    arr[2] = j + 1;
                }
            }
            
            for (int j = 0; j < list2.size(); j++) {//循环第二个金属损失
                Map<String,Object> map = list2.get(j);
                BigDecimal mileage = (BigDecimal)map.get("MILEAGE");
                
                if(scale.compareTo(mileage) == -1 ){
                    list2 = list2.subList(j, list2.size());
                    arr[1] = j ;
                    break;
                }
                else if(j == list2.size() - 1){
                    list2 = Collections.emptyList();
                    arr[1] = j + 1;
                }
            }
            if(arr[1] != null && arr[2] != null && !Integer.valueOf(0).equals(arr[1])){
                c_list.add(arr);
            }
        }
        
        AjaxSupport.sendSuccessText("",c_list);
    }
    
    

    /**
     * 获取环焊缝异常
     * @param integalzQuery
     */
    @RequestMapping("/queryGirthWeld.json")
    @ResponseBody
    public void queryGirthWeld(IntegalzQueryVO integalzQuery,@RequestParam(required = false,value = "inspTaskId")String inspTaskId){
        
        String pipeLineId = integalzQuery.getPipeLineId();
        List<PipeLineNewTaskVO> pipeLineInspTaskList;
        if(StringUtils.isEmpty(inspTaskId)){
            pipeLineInspTaskList = pipeLineInspTaskService.queryNewInspTaskMerge(pipeLineId);
        }else{//指定了内检测任务
            pipeLineInspTaskList = new ArrayList<PipeLineNewTaskVO>();
            PipeLineNewTaskVO pipeLineNewTask = new PipeLineNewTaskVO();
            pipeLineNewTask.setId(inspTaskId);
            pipeLineNewTask.setIncludeStart(true);
            pipeLineNewTask.setIncludeEnd(true);
            pipeLineNewTask.setStartMileage(integalzQuery.getStartMileage());
            pipeLineNewTask.setEndMileage(integalzQuery.getEndMileage());
            pipeLineInspTaskList.add(pipeLineNewTask);
        }
        
        List<Map<String,Object>> list = pipeLineInspTaskService.queryGirthWeld(integalzQuery, pipeLineInspTaskList);
        
        AjaxSupport.sendSuccessText("",list);
    }
    

    /**
     * 获取环焊缝返修
     * @param integalzQuery
     */
    @RequestMapping("/queryGwwRepairind.json")
    @ResponseBody
    public void queryGwwRepairind(IntegalzQueryVO integalzQuery){
        
        List<Map<String,Object>> list = pipeLineInspTaskService.queryGwwRepairind(integalzQuery);
        
        AjaxSupport.sendSuccessText("",list);
    }
    
    /**
     * 获取内检测缺陷（凹陷）
     * @param integalzQuery
     */
    @RequestMapping("/queryInspDefectD.json")
    @ResponseBody
    public void queryInspDefectD(@RequestParam("inspTaskId")String inspTaskId
            ,@RequestParam(required = false,value = "startMileage")BigDecimal startMileage
            ,@RequestParam(required = false,value = "endMileage")BigDecimal endMileage
            ,ModelMap model){
        
        List<Map<String,Object>> list_d = pipeLineInspTaskService.queryInspDefect(InspTableNameEnum.DEPRESSED, inspTaskId,startMileage,endMileage);
        
        AjaxSupport.sendSuccessText("",list_d);
    }
    

    /**
     * 获取内检测缺陷（带下阴影、椭圆变形）
     * @param integalzQuery
     */
    @RequestMapping("/queryInspDefectBE.json")
    @ResponseBody
    public void queryInspDefectBDE(@RequestParam("inspTaskId")String inspTaskId
            ,@RequestParam(required = false,value = "startMileage")BigDecimal startMileage
            ,@RequestParam(required = false,value = "endMileage")BigDecimal endMileage
            ,ModelMap model){
        
        List<Map<String,Object>> list_b = pipeLineInspTaskService.queryInspDefect(InspTableNameEnum.BELOW_SHADOW, inspTaskId,startMileage,endMileage);
        //model.addAttribute(InspTableNameEnum.BELOW_SHADOW.toString(), list_b);
        
        List<Map<String,Object>> list_e = pipeLineInspTaskService.queryInspDefect(InspTableNameEnum.ELLIPTIC, inspTaskId,startMileage,endMileage);
        //model.addAttribute(InspTableNameEnum.ELLIPTIC.toString(), list_e);
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        list.addAll(list_b);
        list.addAll(list_e);
        AjaxSupport.sendSuccessText("",list);
    }
    

    /**
     * 获取内检测缺陷（环焊缝、制造焊缝）
     * @param integalzQuery
     */
    @RequestMapping("/queryInspDefectGP.json")
    @ResponseBody
    public void queryInspDefectGP(@RequestParam("inspTaskId")String inspTaskId
            ,@RequestParam(required = false,value = "startMileage")BigDecimal startMileage
            ,@RequestParam(required = false,value = "endMileage")BigDecimal endMileage
            ,ModelMap model){
        
        List<Map<String,Object>> list_g = pipeLineInspTaskService.queryInspDefect(InspTableNameEnum.GIRTH_WELD, inspTaskId,startMileage,endMileage);
        model.addAttribute(InspTableNameEnum.GIRTH_WELD.toString(), list_g);
        
        List<Map<String,Object>> list_p = pipeLineInspTaskService.queryInspDefect(InspTableNameEnum.PIPE_WELD, inspTaskId,startMileage,endMileage);
        model.addAttribute(InspTableNameEnum.PIPE_WELD.toString(), list_p);
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        list.addAll(list_g);
        list.addAll(list_p);
        AjaxSupport.sendSuccessText("",list);
    }
    
    
    
    

    /**
     * 获取内检测缺陷（金属损失）
     * @param integalzQuery
     */
    @RequestMapping("/queryInspDefectMETA_LOSS.json")
    @ResponseBody
    public void queryInspDefectMETA_LOSS(@RequestParam("inspTaskId")String inspTaskId
            ,@RequestParam(required = false,value = "startMileage")BigDecimal startMileage
            ,@RequestParam(required = false,value = "endMileage")BigDecimal endMileage
            ,ModelMap model){
        
        List<Map<String,Object>> list_g = pipeLineInspTaskService.queryInspDefect(InspTableNameEnum.META_LOSS, inspTaskId,startMileage,endMileage);
        //model.addAttribute(InspTableNameEnum.META_LOSS.toString(), list_g);
        
        AjaxSupport.sendSuccessText("",list_g);
    }
    
    
    
    
    
    
    
    
}
